<?php

namespace app\home\controller;

use think\Controller;
use think\Cookie;


//公共控制器
use app\common\controller\Home AS HomeController;

/**
 * 收藏控制器
 */
class Collect extends HomeController
{
    // 构造函数
    public function __construct()
    {
        //继承底层的控制器构造函数
        parent::__construct();

        //定义一个全局模型
        $this->PostModel = model('Post.Post');
        $this->UserModel = model('User.User');
        $this->CollectModel = model('User.Collect');
        $this->CommentModel = model('Post.Comment');
    }

    //添加收藏
    public function add() {

        $success = [
            'result' => false,
            'msg' => ''
        ];

         //判断是否有post数据
         if($this->request->isPost()){

            $postid = $this->request->param('postid', 0);

            //根据id去查询这个帖子是否存在
            $post = $this->PostModel->find($postid);
            
            if(!$post)
            {
                $this->error('编辑的帖子不存在');
                exit;
            }

             //判断当前这个人是否有收藏过
             $where = [
                'userid' => $this->LoginUser['id'],
                'postid' => $postid
            ];

            $collect = $this->CollectModel->where($where)->find();

            if($collect)
            {
                $success['result'] = false;
                $success['msg'] = '您已收藏此贴，无须重复收藏';
                return json($success);
            }

            //组装数据
            $data =[
                'userid' => $this->LoginUser['id'],
                'postid' => $postid
            ];

            //添加收藏
            $result = $this->CollectModel->validate('common/User/Collect')->save($data);

            if($result === FALSE)
            {
                //添加收藏失败
                $success['result'] = false;
                $success['msg'] = $this->CollectModel->getError();
            }else
            {
                //添加收藏成功
                $success['result'] = true;
                $success['msg'] = '收藏成功';
            }

            //将结果返回给ajax
            return json($success);

         }
    }

    //取消收藏
    public function del() {
        $success = [
            'result' => false,
            'msg' => ''
        ];

         //判断是否有post数据
         if($this->request->isPost()){

            $postid = $this->request->param('postid', 0);

            //根据id去查询这个帖子是否存在
            $post = $this->PostModel->find($postid);
            
            if(!$post)
            {
                $this->error('编辑的帖子不存在');
                exit;
            }


            //组装数据
            $data =[
                'userid' => $this->LoginUser['id'],
                'postid' => $postid
            ];

            //取消收藏
            $result = $this->CollectModel->validate('common/User/Collect')->where($data)->delete();

            if($result === FALSE)
            {
                //取消收藏失败
                $success['result'] = false;
                $success['msg'] = $this->CollectModel->getError();
            }else
            {
                //取消收藏成功
                $success['result'] = true;
                $success['msg'] = '取消收藏成功';
            }

            //将结果返回给ajax
            return json($success);

         }
    }

    //点赞
    public function thumbs() {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isPost())
        {
            $id = $this->request->param('id', 0);
            
            //组装数据
            $data = [
                'id' => $id
            ];
           
            //查询评论数据
            $results = $this->CommentModel->where($data)->find();

            if(!$results)
            {
                $this->error('该评论不存在');
                exit;
            }
        
            $visit = trim($results['like']);
            $VisitArr = explode(',', $visit); //转换为数组
            $VisitArr = array_filter($VisitArr);  //去除数组中的空元素

            $userid = $this->LoginUser['id'];

            if(!in_array($userid, $VisitArr))
            {
                $VisitArr[] = $userid;

                //组装数据   
                $data = [
                    'id'=>$id, //一定要有id
                    'like'=>implode(',', $VisitArr)   //数组转化为字符串
                ];

                //更新语句
                $result = $this->CommentModel->isUpdate(true)->save($data);

                if($result === FALSE)
                {
                    //点赞失败
                    $success['result'] = false;
                    $success['msg'] = $this->CommentModel->getError();
                }else
                {
                    //点赞成功
                    $success['result'] = true;
                    $success['msg'] = '点赞成功';
                }

                //将结果返回给ajax
                return json($success);

            } else {

                //循环数据
                foreach ( $VisitArr as $key=>$value)
                {
                    if ($value == $userid) 
                        //删除数组的用户 
                        unset($VisitArr[$key]);   
                }
                
                // 判断数组的长度是否大于0，是就转成数组字符串，否则为空
                $VisitArr = count($VisitArr) > 0 ? implode(',', $VisitArr) : '';

                //组装数据   
                $data = [
                    'id'=>$id, //一定要有id
                    'like'=>$VisitArr   //数组转化为字符串
                ];

                //更新语句
                $result = $this->CommentModel->isUpdate(true)->save($data);

                if($result === FALSE)
                {
                     //取消点赞失败
                    $success['result'] = false;
                    $success['msg'] = $this->CommentModel->getError();
                }else
                {
                    //取消点赞成功
                    $success['result'] = true;
                    $success['msg'] = '取消点赞成功';
                }

                //将结果返回给ajax
                return json($success);
            }
        }
    }
}